package com.bcx.crm.spring_boot.springMVC.myInterceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Created by ys on 2018/4/20.
 * 1.@EnableWebMvc : 开启SpringMvc的支持,若无此注解则重写的WebMvcConfigureAdapter方法无效
 * 2.继承WebMvConfigurerAdapter,重写其方法可对SpringMVC进行配置
 * 3.配置拦截器的Bean
 * 4.重写该方法,注册拦截器
 * 该处：测试拦截器失败，请求不能执行到DemoInterceptor
 */
@Configuration
@EnableWebMvc //1.
//@ComponentScan("com.bcx.crm.spring.spring_boot.springMVC.myInterceptor")
@ComponentScan("com.bcx.crm.spring_boot.springMVC")
public class MyMvcConfig extends WebMvcConfigurerAdapter { //2.
    @Bean
    public DemoInterceptor getDemoInterceptor(){  //3.
        return new DemoInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {  //4.
        registry.addInterceptor(getDemoInterceptor());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**/api2/**").addResourceLocations("classpath:/spring/");
    }
}
